package com.roadjava.rbac.controller;
//控制器层，处理HTTP请求，调用服务层的方法，并返回响应给客户端。提供了获取推荐列表、获取单个推荐详情、创建推荐、更新推荐和删除推荐的接口。

import com.roadjava.rbac.bean.dto.RecommendationDTO;
import com.roadjava.rbac.bean.vo.RecommendationVO;
import com.roadjava.rbac.service.RecommendationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/api/recommendations")
public class RecommendationController {

    private static final Logger logger = LoggerFactory.getLogger(RecommendationController.class);
    private final RecommendationService recommendationService;

    @Autowired
    public RecommendationController(RecommendationService recommendationService) {
        this.recommendationService = recommendationService;
    }

    @GetMapping
    public ResponseEntity<Page<RecommendationVO>> getRecommendations(
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Long resourceID,
            @RequestParam(required = false) String userBehavior,
            Pageable pageable) {
        logger.info("Fetching recommendations with userId: {}, resourceID: {}, userBehavior: {}", userId, resourceID, userBehavior);
        return ResponseEntity.ok(recommendationService.getRecommendations(userId, resourceID, userBehavior, pageable));
    }

    @GetMapping("/{id}")
    public ResponseEntity<RecommendationVO> getRecommendationById(@PathVariable @NotNull @Min(1) Long id) {
        logger.info("Fetching recommendation by ID: {}", id);
        return recommendationService.getRecommendationById(id)
                .map(ResponseEntity::ok)
                .orElseGet(() -> {
                    logger.warn("Recommendation not found for ID: {}", id);
                    return ResponseEntity.notFound().build();
                });
    }

    @PostMapping
    public ResponseEntity<RecommendationVO> createRecommendation(@Valid @RequestBody RecommendationDTO recommendationDTO) {
        logger.info("Creating new recommendation for user: {}", recommendationDTO.getUserId());
        RecommendationVO recommendationVO = recommendationService.saveRecommendation(recommendationDTO);
        logger.info("Recommendation created with ID: {}", recommendationVO.getId());
        return ResponseEntity.status(201).body(recommendationVO);
    }

    @PutMapping("/{id}")
    public ResponseEntity<RecommendationVO> updateRecommendation(@PathVariable @NotNull @Min(1) Long id,
                                                                 @Valid @RequestBody RecommendationDTO recommendationDTO) {
        logger.info("Updating recommendation with ID: {}", id);
        return recommendationService.updateRecommendation(id, recommendationDTO)
                .map(ResponseEntity::ok)
                .orElseGet(() -> {
                    logger.warn("Recommendation not found for ID: {}", id);
                    return ResponseEntity.notFound().build();
                });
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteRecommendation(@PathVariable @NotNull @Min(1) Long id) {
        logger.info("Deleting recommendation with ID: {}", id);
        boolean success = recommendationService.deleteRecommendation(id);
        return success ? ResponseEntity.ok().<Void>build() : ResponseEntity.notFound().build();
    }
}